import Vue from 'vue';

import App from './App.vue';
import router from './router';
import store from './store/index';
import './assets/main.css';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import 'amfe-flexible';
import '../rem';
import '../src/styles/global.css';

Vue.use(ElementUI);

Object.defineProperty(window, 'localStorage', {
  configurable: true,
  get() {
    console.warn('[拦截] localStorage 被访问');
    return {
      getItem: () => null,
      setItem: () => {},
      removeItem: () => {},
      clear: () => {},
      key: () => null,
      length: 0
    };
  }
});

Object.defineProperty(document, 'cookie', {
  configurable: true,
  get() {
    console.warn('[拦截] document.cookie 被读取');
    return '';
  },
  set(val) {
    console.warn('[拦截] 设置 cookie:', val);
  }
});

Vue.directive("drag", {
  // 当被绑定的元素挂载到 DOM 中时……
  // 拖拽指令的定义
  bind(el, binding, vnode) {
    let oDiv = el; // 当前元素
    // 禁止选择拖拽块上的文字
    document.onselectstart = function () {
      return false;
    };
    oDiv.onmousedown = function (e) {
      // 获取父元素dom
      let pbox = e.target.parentNode;
      // 鼠标按下，计算当前元素距离可视区的距离
      let disX = e.clientX - oDiv.offsetLeft;
      let disY = e.clientY - oDiv.offsetTop;
      document.onmousemove = function (e) {
        // 通过事件委托，计算移动的距离
        let l = e.clientX - disX;
        let t = e.clientY - disY;
        if (l <= 0) {
          l = 0
        } else if (l >= (pbox.clientWidth - oDiv.clientWidth)) {
          l = pbox.clientWidth - (oDiv.clientWidth - 2)
        }
        if (t <= 0) {
          t = 0
        } else if (t >= (pbox.clientHeight - oDiv.clientHeight)) {
          t = pbox.clientHeight - oDiv.clientHeight
        }
        // 移动当前元素
        oDiv.style.left = l + "px";
        oDiv.style.top = t + "px";
      };
      document.onmouseup = function (e) {
        document.onmousemove = null;
        document.onmouseup = null;
      };
      // 防止黏连
      return false;
    };
  }
});

new Vue({
  store,
  // i18n,
  router,
  render: (h) => h(App)
}).$mount('#app');
